package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCartMapper;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.cskaoyan.market.vo.WxCouponVo;
import com.cskaoyan.market.vo.WxMyCouponVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class WxCouponServiceImpl implements WxCouponService {
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCartMapper marketCartMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public List<WxMyCouponVo> myList(MarketUser user, Short status, Integer page, Integer limit) {

        marketCouponUserMapper.updateCouponByNow();

        if (user == null) return null;
        PageHelper.startPage(page, limit);
        List<WxMyCouponVo> list = marketCouponMapper.selectMyCoupon(user.getId(), status);
        return list;
    }

    @Override
    public List<WxCouponVo> list(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<WxCouponVo> result = marketCouponMapper.selectWxCouponVo();
        return result;
    }

    @Override
    public List<WxMyCouponVo> selectList(Integer userId,Integer cartId, Integer grouponRulesId) {

        marketCouponUserMapper.updateCouponByNow();

        if (cartId==0){
            MarketCartExample marketCartExample = new MarketCartExample();
            MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andCheckedEqualTo(true);
            List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);

            double total=0;
            for (MarketCart cart : marketCarts) {
                double v = cart.getPrice().doubleValue();
                total+=v*cart.getNumber();
            }

            List<WxMyCouponVo> list = marketCouponMapper.selectUsefulCoupon(userId, new BigDecimal(total), LocalDateTime.now());
            //目前取出的是不考虑类目限制的优惠券

//            for (WxMyCouponVo wxMyCouponVo : list) {
//                if (wxMyCouponVo.)
//            }

            return list;
        }

        MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);
        userId = marketCart.getUserId();
        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria = marketCartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andCheckedEqualTo(true);
        List<WxMyCouponVo> list = marketCouponMapper.selectUsefulCoupon(userId, marketCart.getPrice().multiply(BigDecimal.valueOf(marketCart.getNumber())), LocalDateTime.now());


        return list;
    }

    @Transactional
    @Override
    public Integer exchange(String i, MarketUser user) {
        if (user==null)return null;
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andCodeEqualTo(i);
        MarketCoupon marketCoupon = marketCouponMapper.selectOneByExample(marketCouponExample);
        if (marketCoupon == null) return 742;
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria1 = marketCouponUserExample.createCriteria();
        criteria1.andCouponIdEqualTo(marketCoupon.getId());
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        if (marketCouponUsers.size()>marketCoupon.getLimit())return 740;


        if (marketCoupon.getTotal() == 1) return 740;//将1定为没有0为无穷个

        if (marketCoupon.getEndTime()!=null&&marketCoupon.getEndTime().isBefore(LocalDateTime.now())) return 744;
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(user.getId());
        marketCouponUser.setCouponId(marketCoupon.getId());
        marketCouponUser.setStatus(marketCoupon.getStatus());
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        marketCouponUser.setEndTime(marketCoupon.getEndTime());
        marketCouponUser.setAddTime(LocalDateTime.now());
        marketCouponUser.setUpdateTime(LocalDateTime.now());
        marketCouponUser.setDeleted(false);

        marketCouponUserMapper.insertSelective(marketCouponUser);
        marketCoupon.setTotal(marketCoupon.getTotal()-1);
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        return 0;
    }

    @Transactional
    @Override
    public Integer receive(Integer couponId, MarketUser user) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(couponId);
        if (marketCoupon==null||marketCoupon.getTotal()==1)return 744;
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        List<MarketCouponUser> marketCouponUsers = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        if (marketCoupon.getLimit()!=0&&marketCouponUsers.size()>marketCoupon.getLimit())return 740;

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setUserId(user.getId());
        marketCouponUser.setCouponId(marketCoupon.getId());
        marketCouponUser.setStatus(marketCoupon.getStatus());
        marketCouponUser.setStartTime(marketCoupon.getStartTime());
        if (marketCoupon.getTimeType()==1) {
            marketCouponUser.setEndTime(marketCoupon.getEndTime());
        }else {
            marketCouponUser.setEndTime(LocalDateTime.now().plusDays(marketCoupon.getDays()));
        }
        marketCouponUser.setAddTime(LocalDateTime.now());
        marketCouponUser.setUpdateTime(LocalDateTime.now());
        marketCouponUser.setDeleted(false);
        marketCouponUserMapper.insertSelective(marketCouponUser);
        if (marketCoupon.getTotal()!=0)
        marketCoupon.setTotal(marketCoupon.getTotal()-1);
        if (marketCoupon.getLimit()!=0)
        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        return 0;

    }
}
